package com.xujianlong.day04;

import com.xujianlong.bean.UserBehavior;
import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;


public class Flink02_Project_PV {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setRuntimeMode(RuntimeExecutionMode.STREAMING);
        env.setParallelism(1);

        DataStreamSource<String> dataStreamSource = env.readTextFile("input/UserBehavior.csv");
        dataStreamSource.process(new ProcessFunction<String, Tuple2<String,Integer>>() {
            private Integer count=0;
            @Override
            public void processElement(String s, Context context, Collector< Tuple2<String,Integer>> collector) throws Exception {
                String[] split = s.split(",");
                UserBehavior userBehavior = new UserBehavior(
                        Long.parseLong(split[0]),
                        Long.parseLong(split[1]),
                        Integer.parseInt(split[2]),
                        split[3],
                        Long.parseLong(split[4])
                );

                if("pv".equals( userBehavior.getBehavior())){
                    count++;
                    collector.collect(Tuple2.of("pv",count));
                }
            }
        }).print();

        env.execute();

    }
}
